import 'package:dio/dio.dart' as dio;
import 'package:flutter/cupertino.dart';
import 'package:search_people/global/user/logic.dart';
import 'package:search_people/utils/storage.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../utils/httputils/net_api.dart';
import '../../utils/httputils/result_data.dart';
import 'state.dart';
import 'package:dio/dio.dart';

class PasswordloginLogic extends GetxController {
  final PasswordloginState state = PasswordloginState();

  @override
  void onInit() async {
    await SpUtil().init().then((value) {
      String phone = SpUtil().getStr('phone');

      state.nameCotroller.text = phone;
    });
    super.onInit();

    // UmengCommonSdk.initCommon('63e9ce8dba6a5259c40175f7', '', 'channel');
    // UmengVerifySdk.setVerifySDKInfo(
    //     'qnJ2vlN6sH0BWKB5G2Nz8dozoF8LcC6mrVpfVxKcQly6VKJtl0qlLY+EqfZwlk/bauWAX7cbK8Goj5O/UrUp0zugT/qok+Yr8VhsFSPweWGBroMv+BVPWf5oiklb2pr1+8XPREbN9Mfa+Dz+3IxDtgjyv9swBLtmp1XoL7Pz5EEc7GIoP7fL7b1nuVXtkvhKF03oHdXRcPkI02uDYwp6jNJeHj1oYxbZuLZw10apdj0jqHwG1qaTEncyNcZ6HUd+69SWLEeloMcpbH4o9bNbmf2xazK+Q2pQd87meXsr7DzkuvN2CCOCV8+1hfMkpt9y',
    //     'iosInfo',
    //
    // );
    // UmengVerifySdk.checkDeviceCellularDataEnable();
  }

  //登录接口

  login() async {
    // print(SpUtil().getStr(key_store_user_token));
    // print(SpUtil().getJSON(key_store_user));
    // return;

    String loginName = state.nameCotroller.text;

    var params = {
      'appCode': "A20211202140446825",
      'loginName': loginName,
      'loginType': state.loginType == 1 ? 2 : 1,
    };

    if (state.loginType == 1) {
      String loginPassword = state.passCotroller.text;
      if (loginName.trim().isEmpty) {
        return EasyLoading.showToast('请输入手机号');
      }

      if (loginPassword.trim().isEmpty) {
        return EasyLoading.showToast('请输入密码');
      }
      params['loginPassword'] = loginPassword;
    } else {
      String smCode = state.smsCotroller.text;
      if (smCode.trim().isEmpty) {
        return EasyLoading.showToast('请输入验证码');
      }
      params['smCode'] = smCode;
    }

    // if (!state.isAgree) {
    //   return EasyLoading.showToast('请阅读并勾选同意用户隐私政策');
    // }

    var response = await Api.login(params);
    if (response.isSuccess) {
      UserController().loginSuccess(response.data);
      Get.back();
    } else {
      EasyLoading.showToast(response.msg);
    }
  }

  Future<bool> sendSms() async {
    if (GetUtils.isPhoneNumber(state.nameCotroller.text)) {
      var params = {
        'userId': -1,
        'postId': -1,
        'userName': -1,
        'appCode': "A20211202140446825",
        'oneParam': state.nameCotroller.text,
        'type': "cryun",
        'check': '0'
      };
      var response = await Api.sendCode(params);
      if (response.status == 200) {
        return true;
      } else {
        EasyLoading.showToast(response.data);
      }

      return false;
    } else {
      EasyLoading.showToast('请输入正确的手机号码');
      return false;
    }
  }

  //发送验证码接口
  sendCode(String phone) async {}

  changeLoginType() {
    if (state.loginType == 1) {
      state.pageController.previousPage(
          duration: const Duration(milliseconds: 300), curve: Curves.easeIn);
      state.loginType = 0;
    } else {
      state.pageController.nextPage(
          duration: const Duration(milliseconds: 300), curve: Curves.easeIn);
      state.loginType = 1;
    }

    update();
  }

  //显示或隐藏密码
  fixPasswordText() {
    state.isShow = !state.isShow;
    update();
  }
}
